package com.flower.controller.buyer;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flower.annotation.Log;
import com.flower.domain.dto.buyer.order.CreateOrderDto;
import com.flower.domain.dto.buyer.order.OrderQueryDto;
import com.flower.domain.dto.buyer.order.PaymentQRCodeDto;
import com.flower.domain.entity.Order;
import com.flower.domain.vo.buyer.OrderDetailVO;
import com.flower.domain.vo.buyer.OrderStatusCountVO;
import com.flower.domain.vo.buyer.OrderVO;
import com.flower.domain.vo.buyer.PaymentQRCodeVO;
import com.flower.domain.vo.buyer.PaymentStatusVO;
import com.flower.service.OrderService;
import com.flower.utils.AjaxResult;
import com.flower.utils.UserContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 购买者端订单管理
 */
@Slf4j
@RestController
@RequestMapping("/buyer/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 获取订单列表
     * @param orderQueryDto
     * @return
     */
    @GetMapping("/list")
    public AjaxResult getOrderList(OrderQueryDto orderQueryDto) {
        log.info("OrderQueryDto,{}", orderQueryDto);
        Long buyerId = UserContext.getUserId();
        Page<OrderVO> orders = orderService.getOrderList(buyerId, orderQueryDto);
        return AjaxResult.success(orders);
    }

    /**
     * 获取订单详情
     * @param orderId
     * @return
     */
    @GetMapping("/detail/{orderId}")
    public AjaxResult getOrderDetail(@PathVariable Integer orderId) {
        Long buyerId = UserContext.getUserId();
        OrderDetailVO orderDetail = orderService.getOrderDetail(orderId, buyerId);
        return AjaxResult.success(orderDetail);
    }

    /**
     * 创建订单
     * @param createOrderDto
     * @return
     */
    @Log(name = "创建订单")
    @PostMapping("/create")
    public AjaxResult createOrder(@RequestBody CreateOrderDto createOrderDto) {
        createOrderDto.setBuyerId(UserContext.getUserId().intValue());
        Order order = orderService.createOrder(createOrderDto);
        return AjaxResult.success(order);
    }

    /**
     * 支付订单
     * @param orderId
     * @return
     */
    @Log(name = "支付订单")
    @PutMapping("/pay/{orderId}")
    public AjaxResult payOrder(@PathVariable Integer orderId) {
        orderService.payOrder(orderId);
        return AjaxResult.success();
    }

    /**
     * 取消订单
     * @param orderId
     * @return
     */
    @Log(name = "取消订单")
    @PutMapping("/cancel/{orderId}")
    public AjaxResult cancelOrder(@PathVariable Integer orderId) {
        Long buyerId = UserContext.getUserId();
        orderService.cancelOrder(orderId, buyerId);
        return AjaxResult.success();
    }

    /**
     * 删除订单
     * @param orderId
     * @return
     */
    @Log(name = "删除订单")
    @DeleteMapping("/delete/{orderId}")
    public AjaxResult deleteOrder(@PathVariable Integer orderId) {
        Long buyerId = UserContext.getUserId();
        orderService.deleteOrder(orderId, buyerId);
        return AjaxResult.success();
    }

    /**
     * 确认收货
     * @param orderId
     * @return
     */
    @Log(name = "确认收货")
    @PutMapping("/confirm/{orderId}")
    public AjaxResult confirmReceive(@PathVariable Integer orderId) {
        Long buyerId = UserContext.getUserId();
        orderService.confirmReceipt(orderId, buyerId);
        return AjaxResult.success();
    }

    /**
     * 获取支付二维码
     * @param paymentQRCodeDto
     * @return
     */
    @PostMapping("/payment/qrcode")
    public AjaxResult getPaymentQRCode(@RequestBody PaymentQRCodeDto paymentQRCodeDto) {
        Long buyerId = UserContext.getUserId();
        PaymentQRCodeVO qrCode = orderService.getPaymentQRCode(paymentQRCodeDto, buyerId);
        return AjaxResult.success(qrCode);
    }

    /**
     * 检查支付状态
     * @param orderId
     * @return
     */
    @GetMapping("/payment/status/{orderId}")
    public AjaxResult checkPaymentStatus(@PathVariable Integer orderId) {
        Long buyerId = UserContext.getUserId();
        PaymentStatusVO status = orderService.checkPaymentStatus(orderId, buyerId);
        return AjaxResult.success(status);
    }

    /**
     * 获取订单状态数量
     * @return
     */
    @GetMapping("/status/count")
    public AjaxResult getOrderStatusCount() {
        Long buyerId = UserContext.getUserId();
        OrderStatusCountVO statusCount = orderService.getOrderStatusCount(buyerId);
        return AjaxResult.success(statusCount);
    }
} 